/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/
#include <typedefs.h>
#include <FreeRTOS.h>
#include <lpc210x.h>
#include <task.h>
#include <modv1.h>
#include <board_config.h>
#include <eventq.h>
#include <serial_eventq.h>
#include <serial.h>
#include <stdlib.h>

extern eventq_ops serial_eventq_ops;

#define PRI_SERIAL_TEST     ( tskIDLE_PRIORITY + 1 )

static void serial_test( void *params);
static void serial_test2( void *params);
extern int main ()__attribute__ ((section (".main")));

extern void start();


int main()
{
    int i,j;

    modv1_hw_init();


    eventq_subsys_init();
    eventq_register( "/dev/serial", &serial_eventq_ops, 1 );

    xTaskCreate( serial_test2, ( signed portCHAR * ) "Serial-Test2", 128, NULL, PRI_SERIAL_TEST, NULL );

    xTaskCreate( serial_test, ( signed portCHAR * ) "Serial-Test", 512, NULL, PRI_SERIAL_TEST, NULL );

    /* Now all the tasks have been started - start the scheduler.

    NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
    The processor MUST be in supervisor mode when vTaskStartScheduler is
    called.  The demo applications included in the FreeRTOS.org download switch
    to supervisor mode prior to main being called.  If you are not using one of
    these demo application projects then ensure Supervisor mode is used here. */

    vTaskStartScheduler();

    return 0;
}
static void serial_test( void *params)
{
    static HEVENTQ hserial;
    int32               status;
    static event                e;
    static serial_event_hdr     ehdr;
    int i,j, k;

    uint8 *welcome = "Aum Amriteswaryai Namah\r\n";
    uint8 *prompt = "\r\ncmd: ";
    uint8 buf[16];

    status = eventq_open( "/dev/serial", "serial_test", &hserial );

    e.type = EVT_SER_OPEN;
    ehdr.baud = 9600;
    ehdr.port = 0;
    ehdr.bits = 8;
    ehdr.parity = 0;
    ehdr.mode = 'M';
    e.param = (HPARAM)&ehdr;

    status = eventq_sync_put(hserial, &e );

    e.type = EVT_SER_SEND_DATA;
    e.nbuf = strlen(welcome);
    e.ndata = e.nbuf;
    e.buf = welcome;
    status = eventq_sync_put(hserial, &e );

    k=1;
    while(1)
    {

        e.type = EVT_SER_SEND_DATA;
        e.nbuf = strlen(prompt);
        e.ndata = e.nbuf;
        e.buf = prompt;
        status = eventq_sync_put(hserial, &e );

        if(k)
        {
            GPIO_FIO0SET = BOARD_CONFIG_LED0;
            k=0;
        }else
        {
            GPIO_FIO0CLR = BOARD_CONFIG_LED0;
            k=1;
        }
        e.type = EVT_SER_RECV_DATA;
        e.nbuf = sizeof(buf);
        e.ndata = 0;
        e.buf = buf;
        status = eventq_sync_get(hserial, &e );

        for(i=0;i<e.ndata;i++)
        {
            buf[i] = toupper(buf[i]);
        }
        e.type = EVT_SER_SEND_DATA;
        status = eventq_sync_put(hserial, &e );

    }
}

static void serial_test2( void *params)
{
    int i,j, k;

    k=1;
    while(1)
    {
        if(k)
        {
            GPIO_FIO0CLR = BOARD_CONFIG_LED1;
            k=0;
        }else
        {
            GPIO_FIO0SET = BOARD_CONFIG_LED1;
            k=1;
        }
        /*for(i=0;i<1000;i++)
        {
            for(j=0;j<3000;j++);
        }*/

    }
}
